package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.erp.ErpPurchaseType;
import com.nuoniu.sibanyun.entity.erp.in.InRdStyle;
import com.nuoniu.sibanyun.entity.po.VPoPurchaseOrderMain;
import com.nuoniu.sibanyun.mapper.erp.ErpPurchaseTypeMapper;
import com.nuoniu.sibanyun.mapper.po.VPoPurchaseOrderMainMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpPurchaseTypeService;
import com.nuoniu.sibanyun.service.erp.in.IInRdStyleService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.ApiOperation;
import org.apache.xmlbeans.impl.xb.xmlconfig.Usertypeconfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 采购类型管理表 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2020-12-08
 */
@Service
public class ErpPurchaseTypeServiceImpl extends ServiceImpl<ErpPurchaseTypeMapper, ErpPurchaseType> implements IErpPurchaseTypeService {

    @Autowired
    private IErpAccountService accountService;
    @Autowired
    private VPoPurchaseOrderMainMapper orderMainMapper;
    @Autowired
    private IInRdStyleService rdStyleService;

    @Override
    public Page<ErpPurchaseType> pageList(Page pages, ErpPurchaseType purchaseType) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpPurchaseType> wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        wrapper.eq("account_id", accountService.getCurrentAccountId());
        if(StringUtils.isNotBlank(purchaseType.getSearchKey())) {
            wrapper.and(queryWrapper  -> queryWrapper.likeRight("code",  purchaseType.getSearchKey()).or().like("name", purchaseType.getSearchKey()));
        }
        Page page = page(pages, wrapper);
        return page;
    }

    @Override
    public void savePurchaseType(ErpPurchaseType purchaseType) {
        if(purchaseType.getId() != null) {
            List<VPoPurchaseOrderMain>  orderMains = orderMainMapper.getByPurchaseTypeId(purchaseType.getId());
            if(orderMains.size() > 0) {
                throw new NuoNiuException("已在采购单据使用，不能修改");
            }
        }
        if(isRepeatCode(purchaseType.getCode(),purchaseType.getId()))throw new NuoNiuException("编号重复");
        if(isRepeatName(purchaseType.getName(),purchaseType.getId()))throw new NuoNiuException("名称重复");

//        InRdStyle rdStyle = rdStyleService.getById(purchaseType.getStyleId());
//        purchaseType.setStyleName(rdStyle.getName());
        purchaseType.setCompanyId(UserThreadLocal.get().getCompanyId());
        purchaseType.setAccountId(accountService.getCurrentAccountId());
        if(purchaseType.getIsDefaultValue() != null && purchaseType.getIsDefaultValue()) {
            // 重置默认值
            baseMapper.updateDefaultValue(accountService.getCurrentAccountId());
        }
        this.saveOrUpdate(purchaseType);
    }

    @Override
    public void deleteById(List<Long> ids) {
        for(Long id : ids) {
            List<VPoPurchaseOrderMain>  orderMains = orderMainMapper.getByPurchaseTypeId(id);
            if(orderMains.size() > 0) {
                throw new NuoNiuException("已使用，不能删除");
            }
        }

        this.removeByIds(ids);
    }

    @Override
    public Integer getByStyle(Long id) {
        Integer accountId = accountService.getCurrentAccountId();
        return baseMapper.getByStyle(id, accountId);
    }

    @Override
    public boolean isRepeatCode(String code,Long oldId) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpPurchaseType> queryWrapper =  new QueryWrapper();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("code",code);
        if(null != oldId){
            queryWrapper.ne("id",oldId);
        }
        int count = count(queryWrapper);
        return count > 0;
    }

    @Override
    public boolean isRepeatName(String name,Long oldId) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpPurchaseType> queryWrapper =  new QueryWrapper();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("name",name);
        if(null != oldId){
            queryWrapper.ne("id",oldId);
        }
        int count = count(queryWrapper);
        return count > 0;
    }
}
